package com.hhsj.design.pullableview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hhsj.design.pullableview.view.IFooterLineView;

public class DefaultFooterLineView extends FrameLayout implements IFooterLineView {

    private TextView lineText;

    public DefaultFooterLineView(@NonNull Context context) {
        this(context, null);
    }

    public DefaultFooterLineView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DefaultFooterLineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        lineText = new TextView(getContext());
        lineText.setText("--- 我是有底线的 ---");
        LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        lineText.setPadding(100, 20, 100, 20);
        lineText.setLayoutParams(layoutParams);
        lineText.setGravity(Gravity.CENTER);
        addView(lineText);
        setMinimumHeight(100);

        ViewTreeObserver observer = getViewTreeObserver();
        if (null != observer) {
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    ViewTreeObserver observer = getViewTreeObserver();
                    if (null != observer) {
                        observer.removeOnGlobalLayoutListener(this);
                    }
                }
            });
        }
    }

    @Override
    public void setText(String... text) {

    }

    @Override
    public void setImage(String... image) {

    }

    @Override
    public void showLine() {
        setVisibility(VISIBLE);
    }

    @Override
    public void hideLine() {
        setVisibility(GONE);
    }

    @Override
    public View getFootLineView() {
        return this;
    }
}
